package com.denlaku.longan.controller.impl;

import com.denlaku.longan.anno.Audit;
import com.denlaku.longan.audit.AuditVariables;
import com.denlaku.longan.controller.UserController;
import com.denlaku.longan.core.HttpSessionManager;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.UserQuery;
import com.denlaku.longan.service.UserService;
import com.denlaku.longan.util.RsaUtils;
import com.denlaku.longan.vo.PageBy;
import com.denlaku.longan.vo.User;
import com.github.pagehelper.PageHelper;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * 用户Controller
 *
 * @author tianx
 */
@RestController
@RequestMapping("/common/user")
public class UserControllerImpl implements UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询用户信息
     *
     * @param userQuery 用户查询参数
     * @param pageBy 分页信息
     * @return 用户信息分页列表
     */
    @Override
    public Return<List<User>> pageList(@RequestBody UserQuery userQuery, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<User> list = userService.list(userQuery);
        return Resp.success(list);
    }

    @Override
    public Return<List<User>> search(UserQuery userQuery) {
        List<User> list = userService.search(userQuery);
        return Resp.success(list);
    }

    /**
     * 获取用户信息
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @Override
    public Return<User> get(Long id) {
        User user = userService.get(id);
        return Resp.success(user);
    }

    @Override
    @Audit(module = "User", operation = "audit.operation.addUser", message = "add user: {#user.account}")
    public Return<Long> add(@RequestBody User user) {
        decryptPwd(user);
        userService.add(user);
        return Resp.success(user.getId());
    }

    private void decryptPwd(User user) {
        String password = user.getPassword();
        if (StringUtils.isNotEmpty(password)) {
            user.setPassword(RsaUtils.decrypt(password));
        }
    }

    /**
     * 更新用户
     *
     * @param user 用户信息
     * @return 响应对象
     */
    @Override
    @Audit(module = "User", operation = "audit.operation.updateUser", message = "update user: {#user.account}")
    public Return<Void> update(@RequestBody User user) {
        decryptPwd(user);
        userService.update(user);
        return Resp.success();
    }

    /**
     * 响应对象
     *
     * @param id 用户ID
     * @return 响应对象
     */
    @Override
    @Audit(module = "User", operation = "audit.operation.deleteUser", message = "delete user: {#account}")
    public Return<Void> delete(Long id) {
        setVariable(id);
        userService.delete(id);
        return Resp.success();
    }

    private void setVariable(Long id) {
        User user = userService.get(id);
        String account = Optional.ofNullable(user).map(User::getAccount).orElse(String.valueOf(id));
        AuditVariables.set("account", account);
    }

    /**
     * 启用用户
     *
     * @param id 用户ID
     * @return 响应对象
     */
    @Override
    @Audit(module = "User", operation = "audit.operation.enabledUser", message = "enabled user: {#account}")
    public Return<Void> enabled(Long id) {
        setVariable(id);
        userService.enabled(id);
        return Resp.success();
    }

    /**
     * 禁用用户
     *
     * @param id 用户ID
     * @return 响应对象
     */
    @Override
    @Audit(module = "User", operation = "audit.operation.disabledUser", message = "disabled user: {#account}")
    public Return<Void> disabled(Long id) {
        setVariable(id);
        userService.disabled(id);
        HttpSession httpSession = HttpSessionManager.getSession(id);
        HttpSessionManager.removeSession(id);
        Optional.ofNullable(httpSession).ifPresent(HttpSession::invalidate);
        return Resp.success();
    }

}
